package com.iflytek.jobs.service.config;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.iflytek.jobs.service.domain.vo.FollowRecordImportDTO;
import com.iflytek.jobs.service.service.impl.FollowRecordServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class FollowRecordImportListener extends AnalysisEventListener<FollowRecordImportDTO> {
    /**
     * 批处理 阈值
     */
    private static final int BATCH_COUNT = 50;
    /**
     * 批处理集合
     */
    private List<FollowRecordImportDTO> dataList = new ArrayList<>();
    @Autowired
    private FollowRecordServiceImpl followRecordService;
    /**
     * 解析每一条数据
     * @param data    follow record import dto
     * @param context analysis context
     */
    @Override
    public void invoke(FollowRecordImportDTO data, AnalysisContext context) {
        if (data.getNickName() == null || data.getVisitTime() == null) {
            throw new RuntimeException("用户姓名或回访时间不能为空，行号：" + context.readRowHolder().getRowIndex());
        }
        dataList.add(data);
        // 达到批量阈值，执行入库
        if (dataList.size() >= BATCH_COUNT) {
            saveData();
            dataList.clear(); // 清空列表
        }
    }

    /**
     * 处理完所有数据
     * @param analysisContext analysis context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    /**
     * 批量数据导入
     */
    public void  saveData() {

    }
}
